import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/box/conteroller/Invite_box_controller.dart';
import 'package:nft_shop/box/model/models.dart';
import 'package:nft_shop/box/widget/Invitebox/dialog_gain_box.dart';
import 'package:nft_shop/box/widget/Invitebox/dialog_my_chum.dart';
import 'package:nft_shop/box/widget/Invitebox/dialog_open_box.dart';
import 'package:nft_shop/box/widget/purchase_instructions_widget.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/util/other_utils.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:nft_shop/widgets/status_view.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:share/share.dart';

class InviteBoxPage extends StatefulWidget {
  final String ID;

  InviteBoxPage({Key? key, required this.ID}) : super(key: key);

  @override
  _InviteBoxPageState createState() => _InviteBoxPageState();
}

class _InviteBoxPageState extends State<InviteBoxPage> {
  InviteBoxController controller = InviteBoxController.to;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    controller.getInviteInfo(widget.ID);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<InviteBoxController>(
        id: InviteBoxController.InviteBoxInfo,
        builder: (logic) {
          if (logic.status != LoadStatusType.success) {
            return Scaffold(
                appBar: MyAppBar(
                  backgroundColor: Color(0xFF0F1115),
                  backImgColor: Colors.white,
                ),
                body: Container(
                    color: Color(0xFF0F1115),
                    child: StatusViews(logic.status)));
          }
          return Scaffold(
            body: Container(
              height: Get.height,
              color: Color(0xFF0F1115),
              child: MyScrollView(
                children: [
                  Stack(
                    children: [
                      LoadAssetImage(
                        "Invite_box_top",
                        fit: BoxFit.fitWidth,
                      ),
                      MyAppBar(
                        backgroundColor: Colors.transparent,
                        backImgColor: Colors.white,
                      ),
                    ],
                  ),
                  titleTop("藏品内容"),
                  collectionContent(),
                  //好友
                  cumulativeFriendsTop(),
                  cumulativeFriendsWidget(),
                  titleTop("活动规则"),
                  activityRulesWidget(),
                  SizedBox(
                    height: 20,
                  )
                ],
                bottomButton: bottomWidget(),
              ),
            ),
          );
        });
  }

  //藏品内容
  Widget collectionContent() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14),
      child: GridView.builder(
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        padding: EdgeInsets.only(
          top: 10,
        ),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3,
          mainAxisSpacing: 15,
          crossAxisSpacing: 15,
          childAspectRatio: 105 / 124,
        ),
        itemBuilder: (context, index) {
          return buidlItem(controller.infoModel!.list![index]);
        },
        itemCount: controller.infoModel!.list!.length,
      ),
    );
  }

  Widget buidlItem(ListElement model) {
    Color color;
    Color borderColor;
    Gradient backColor;
    if (model.tag == "史诗") {
      color = Global.kTheme.textPrimary5;
      borderColor = Color(0xFFFEE1B4);
      backColor = LinearGradient(colors: [
        Color(0xFFFE7B08),
        Color(0xFFF44B02),
      ], begin: Alignment.centerLeft, end: Alignment.centerRight);
    } else if (model.tag == "稀有") {
      color = Global.kTheme.textPrimary5;
      borderColor = Color(0xFFBDB1FF);
      backColor = LinearGradient(colors: [
        Color(0xFF6550EF),
        Color(0xFFBA2FE3),
      ], begin: Alignment.centerLeft, end: Alignment.centerRight);
    } else {
      color = Global.kTheme.textPrimary1;
      borderColor = Global.kTheme.textPrimary5;
      backColor = LinearGradient(colors: [
        Color(0xFFFFFFFF),
        Color(0xFFFFFFFF),
      ], begin: Alignment.centerLeft, end: Alignment.centerRight);
    }
    return Container(
      width: double.infinity,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8),
        border: Border.all(width: 2, color: Color(0xFFB94CA9)),
      ),
      child: Stack(
        children: [
          ClipRRect(
              borderRadius: BorderRadius.circular(5),
              child: LoadImage(
                model.img ?? "",
                fit: BoxFit.fitHeight,
                width: double.infinity,
                height: double.infinity,
              )),
          Positioned(
            right: 0,
            top: 0,
            child: Container(
              width: Get.width / 3 - 20 - 4,
              padding: EdgeInsets.symmetric(horizontal: 10, vertical: 4),
              decoration: BoxDecoration(
                  gradient: backColor,
                  border: Border.all(width: 1, color: borderColor),
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(6),
                    topRight: Radius.circular(6),
                    topLeft: Radius.circular(6),
                    bottomRight: Radius.circular(6),
                  )),
              child: Text(
                model.tag ?? "",
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                    fontSize: 14, fontWeight: FontWeight.w600, color: color),
              ),
            ),
          ),
          Positioned(
            left: 0,
            right: 0,
            bottom: 0,
            child: ClipRRect(
              borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(5),
                  bottomRight: Radius.circular(5)),
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 6, vertical: 3),
                color: Colors.black.withAlpha(43),
                child: Text(
                  model.title ?? "",
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  textAlign: TextAlign.left,
                  style: TextStyle(
                      fontSize: 12, color: Global.kTheme.textPrimary5),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  //累计好友头
  Widget cumulativeFriendsTop() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 20),
      child: Stack(
        children: [
          titleTop("累计好友${controller.infoModel!.userNum}位"),
          Positioned(
            right: 0,
            top: 15,
            child: InkWell(
              onTap: () {
                Utils.showElasticDialog<void>(
                  context: context,
                  builder: (BuildContext context) {
                    return DialogMyChum(
                      ID: widget.ID,
                    );
                  },
                );
              },
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 9, vertical: 7),
                decoration: BoxDecoration(
                    gradient: LinearGradient(colors: [
                      Color(0xFFF7D39D),
                      Color(0xFFFDC360),
                    ], begin: Alignment.centerLeft, end: Alignment.centerRight),
                    borderRadius: BorderRadius.circular(100)),
                child: Text(
                  "好友列表",
                  style: TextStyle(
                    fontSize: 14,
                    color: Color(0xFF9A3713),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  //累计好友
  Widget cumulativeFriendsWidget() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 10),
      decoration: BoxDecoration(
        color: Color(0xFF12193E),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: Color(0xFF182252), width: 3),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            height: 44,
            padding: EdgeInsets.symmetric(horizontal: 12),
            decoration: BoxDecoration(
              color: Color(0xFF1F2B66),
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(8), topRight: Radius.circular(8)),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                    flex: 1,
                    child: Text(
                      "邀请人数",
                      textAlign: TextAlign.left,
                      maxLines: 1,
                      style: TextStyle(
                        fontSize: 14,
                        color: Global.kTheme.textPrimary5,
                      ),
                    )),
                Expanded(
                    flex: 2,
                    child: Text(
                      "奖励",
                      textAlign: TextAlign.center,
                      maxLines: 1,
                      style: TextStyle(
                        fontSize: 14,
                        color: Global.kTheme.textPrimary5,
                      ),
                    )),
                Expanded(
                    flex: 1,
                    child: Text(
                      "限量",
                      textAlign: TextAlign.center,
                      maxLines: 1,
                      style: TextStyle(
                        fontSize: 14,
                        color: Global.kTheme.textPrimary5,
                      ),
                    )),
                Expanded(
                    flex: 1,
                    child: Text(
                      "剩余",
                      textAlign: TextAlign.center,
                      maxLines: 1,
                      style: TextStyle(
                        fontSize: 14,
                        color: Global.kTheme.textPrimary5,
                      ),
                    )),
                Expanded(
                    flex: 1,
                    child: Text(
                      "状态",
                      textAlign: TextAlign.right,
                      maxLines: 1,
                      style: TextStyle(
                        fontSize: 14,
                        color: Global.kTheme.textPrimary5,
                      ),
                    )),
              ],
            ),
          ),
          ListView.builder(
            shrinkWrap: true,
            padding: EdgeInsets.zero,
            physics: NeverScrollableScrollPhysics(),
            itemCount: controller.infoModel!.rule!.length,
            itemBuilder: (_, index) {
              if (index == controller.infoModel!.rule!.length - 1) {
                return ClipRRect(
                    borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(8),
                        bottomRight: Radius.circular(8)),
                    child: cumulativeFriendsItem(
                        controller.infoModel!.rule![index], index));
              } else {
                return cumulativeFriendsItem(
                    controller.infoModel!.rule![index], index);
              }
            },
          ),
        ],
      ),
    );
  }

  //累计好友Item
  Widget cumulativeFriendsItem(Rule model, int index) {
    return Container(
      height: 47,
      margin: EdgeInsets.zero,
      padding: EdgeInsets.symmetric(horizontal: 12),
      decoration: BoxDecoration(
          border: Border(
        bottom: Divider.createBorderSide(context,
            color: Color(0xFF172252), width: 1),
      )),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
              flex: 1,
              child: Text(
                "邀请${model.numberEnd}位",
                // "第${model.numberEnd}-${model.numberEnd}位",
                textAlign: TextAlign.left,
                maxLines: 1,
                style: TextStyle(
                  fontSize: 14,
                  color: Global.kTheme.textPrimary5,
                ),
              )),
          Expanded(
              flex: 2,
              child: Text(
                "${model.title}一次",
                textAlign: TextAlign.center,
                maxLines: 1,
                style: TextStyle(
                  fontSize: 14,
                  color: Global.kTheme.textPrimary5,
                ),
              )),
          Expanded(
              flex: 1,
              child: Text(
                "${model.num ?? "0"}",
                textAlign: TextAlign.center,
                maxLines: 1,
                style: TextStyle(
                  fontSize: 14,
                  color: Global.kTheme.textPrimary5,
                ),
              )),
          // "${model.nousednum ?? "0"}",
          Expanded(
              flex: 1,
              child: Text(
                "有剩余",
                textAlign: TextAlign.center,
                maxLines: 1,
                style: TextStyle(
                  fontSize: 14,
                  color: Global.kTheme.textPrimary5,
                ),
              )),
          Expanded(
              flex: 1,
              child: Text(
                model.status == 0 ? "未完成" : "已完成",
                textAlign: TextAlign.right,
                maxLines: 1,
                style: TextStyle(
                  fontSize: 14,
                  color: Global.kTheme.textPrimary5,
                ),
              )),
        ],
      ),
    );
  }

  //标题头
  Widget titleTop(String title) {
    return Center(
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 15),
        child: Stack(
          children: [
            Container(
              margin: EdgeInsets.only(top: 5),
              child: LoadAssetImage(
                "box_title",
                height: 26,
              ),
            ),
            Positioned(
                left: 20,
                right: 20,
                child: Text(
                  title,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      fontSize: 20, color: Global.kTheme.textPrimary5),
                ))
          ],
        ),
      ),
    );
  }

  //活动规则
  Widget activityRulesWidget() {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.symmetric(
        horizontal: 10,
      ),
      padding: EdgeInsets.only(left: 13, right: 15, top: 15, bottom: 10),
      decoration: BoxDecoration(
        color: Color(0xFF12193E),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: Color(0xFF182252), width: 3),
      ),
      child: PurchaseInWidget(
        htmlText: controller.infoModel!.detail ?? "",
      ),
      // Text(
      //   controller.infoModel!.detail??"",
      //   style: TextStyle(height: 2.2, fontSize: 14,color: Global.kTheme.textPrimary5,fontWeight: FontWeight.w600),
      // ),
    );
  }

  //底部
  Widget bottomWidget() {
    return Container(
      color: Color(0xFF000000),
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 10, bottom: 9),
            child: Center(
                child: Text(
              "邀请好友拆盲盒，100%中奖",
              style: TextStyle(
                fontSize: 14,
                color: Global.kTheme.textPrimary5,
              ),
            )),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 15),
            child: Row(
              children: [
                InkWell(
                  onTap: () {
                    Share.share(
                        controller.infoModel!.url ??
                            "${Global.AppHost + "download"}",
                        subject: Global.AppName);
                  },
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 25, vertical: 10),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(12),
                      border: Border.all(color: Color(0xFFB7783B), width: 2),
                      gradient: LinearGradient(
                          colors: [
                            Color(0xFFFDFCDD),
                            Color(0xFFFCCD7D),
                          ],
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight),
                    ),
                    child: Text(
                      "邀请好友拆盲盒",
                      style: TextStyle(fontSize: 18, color: Color(0xFFA55A0C)),
                    ),
                  ),
                ),
                SizedBox(
                  width: 10,
                ),
                Expanded(
                  child: InkWell(
                    onTap: () {
                      if (controller.residue == 0) {
                        EasyLoading.showToast("拆盲盒次数剩余为0，前去邀请好友增加次数吧");
                        return;
                      }

                      showDialog<void>(
                          context: context,
                          builder: (_) => DialogOpenBox(
                                ID: widget.ID,
                                onCall: () async {
                                  await EasyLoading.show(
                                    status: 'loading...',
                                  );
                                  controller.openPackage(widget.ID).then((_) {
                                    EasyLoading.dismiss();
                                    Get.back();
                                    if (controller.openStatus ==
                                        LoadStatusType.success) {
                                      setState(() {});
                                      showDialog<void>(
                                          context: context,
                                          builder: (_) => DialogGainBox(
                                              model: controller.openModel!));
                                    }
                                  });
                                },
                              ));
                    },
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 10),
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(12),
                          color: Global.kTheme.textPrimary5),
                      child: Text(
                        "拆盲盒剩${controller.residue}次",
                        textAlign: TextAlign.center,
                        style:
                            TextStyle(fontSize: 18, color: Color(0xFF333333)),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(
            height: MediaQuery.of(context).padding.bottom,
          )
        ],
      ),
    );
  }
}
